package com.example.job_backend.service.impl;

import com.aliyun.oss.OSS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.job_backend.mapper.ResumeMapper;
import com.example.job_backend.mapper.ResumeTemplateMapper;
import com.example.job_backend.model.Query.ResumeQuery;
import com.example.job_backend.model.Query.ResumeRequest;
import com.example.job_backend.model.R;
import com.example.job_backend.model.Resume;
import com.example.job_backend.model.ResumeData;
import com.example.job_backend.model.ResumeTemplate;
import com.example.job_backend.service.ResumeService;
import com.example.job_backend.util.AliOSSUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sushengren.easyword.EasyWord;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume> implements ResumeService {
    @Resource
    private ResumeMapper resumeMapper;

    @Autowired
    private ResumeTemplateMapper resumeTemplateMapper;

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @Override
    public List<Resume> findResumeByUsername(Integer student_id) {
        QueryWrapper<Resume> resumeQueryWrapper = new QueryWrapper<>();
        resumeQueryWrapper.eq("student_id", student_id);
        return resumeMapper.selectList(resumeQueryWrapper);
    }

    @Override
    public PageInfo<Resume> getResumeByPage(ResumeQuery query) {
        PageHelper.startPage(query.getPage(), query.getPageSize());
        QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", query.getResumeName())
                .eq("student_id",query.getStudentId());
        List<Resume> list = baseMapper.selectList(queryWrapper);
        PageInfo<Resume> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public String createResume(ResumeRequest resumeData) throws IOException {
        //获取模板信息
        ResumeTemplate resumeTemplate = resumeTemplateMapper.selectById(resumeData.getTemplateId());
        String templateUrl = resumeTemplate.getFilePath();

        //下载模板到临时文件
        File templateFile = aliOSSUtils.downloadByUrl(templateUrl);

        //生成简历到临时文件
        File outputFile = generateResumeFile(resumeData, templateFile);

        //上传到oss
        String resumeUrl = aliOSSUtils.uploadLocalFile(outputFile);

        //保存记录
        Resume resume = new Resume();
        resume.setTitle(resumeData.getResumeName());
        resume.setFilePath(resumeUrl);
        resume.setStudentId(resumeData.getStudentId());
        resume.setStatus("已启用");
        resumeMapper.insert(resume);

        // 6. 清理临时文件
        templateFile.delete();
        outputFile.delete();

        return resumeUrl;
    }

    private File generateResumeFile(ResumeRequest data, File template) throws IOException {
        File outputFile = File.createTempFile("resume_", ".docx");

        try (FileOutputStream fos = new FileOutputStream(outputFile)) {
            EasyWord.of(template)
                    .doWrite(data)
                    .toOutputStream(fos);
        }

        return outputFile;
    }
}
